﻿using System;
using System.Windows;
using System.Windows.Controls.Primitives;

namespace BezierSegmentDemo
{
    /// <summary>
    /// Inherit Thumb control to be able to update a Point dependency property while the thumb
    /// is being dragged.
    /// </summary>
    public class ThumbPoint : Thumb
    {
        #region Point
        /// <summary>
        /// Point Dependency Property
        /// </summary>
        public static readonly DependencyProperty PointProperty = DependencyProperty.Register(
            "Point",
            typeof(Point),
            typeof(ThumbPoint),
            new FrameworkPropertyMetadata(
                new Point(),
                new PropertyChangedCallback(PointPropertyChanged)));

        private static void PointPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ThumbPoint tp = (ThumbPoint)d;
            if (tp == null)
            {
                return;
            }

            tp.OnPointChanged((Point)e.OldValue, (Point)e.NewValue);
        }

        /// <summary>
        /// Gets or sets the Point property
        /// </summary>
        public Point Point
        {
            get { return (Point)GetValue(PointProperty); }
            set { SetValue(PointProperty, value); }
        }
        #endregion

        #region MoveDirection
        public MoveDirections MoveDirection
        {
            get { return (MoveDirections)GetValue(MoveDirectionProperty); }
            set { SetValue(MoveDirectionProperty, value); }
        }

        // Using a DependencyProperty as the backing store for MoveDirection.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty MoveDirectionProperty =
            DependencyProperty.Register("MoveDirection", typeof(MoveDirections), typeof(ThumbPoint), new PropertyMetadata(MoveDirections.Both));
        #endregion

        static ThumbPoint()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ThumbPoint), new FrameworkPropertyMetadata(typeof(ThumbPoint)));
        }

        public ThumbPoint()
        {
            this.DragDelta += new DragDeltaEventHandler(this.OnDragDelta);
        }

        private void OnDragDelta(object sender, DragDeltaEventArgs e)
        {
            switch (this.MoveDirection)
            {
                case MoveDirections.Both:
                    this.Point = new Point(this.Point.X + e.HorizontalChange, this.Point.Y + e.VerticalChange);
                    break;
                case MoveDirections.X:
                    this.Point = new Point(this.Point.X + e.HorizontalChange, this.Point.Y);
                    break;
                case MoveDirections.Y:
                    this.Point = new Point(this.Point.X, this.Point.Y + e.VerticalChange);
                    break;
                default:
                    this.Point = new Point(this.Point.X + e.HorizontalChange, this.Point.Y + e.VerticalChange);
                    break;
            }
        }

        #region 位置改变事件
        public delegate void PointChangedHandler(object sender, Point oldPoint, Point newPoint);

        public event PointChangedHandler PointChanged;

        public void OnPointChanged(Point oldPoint, Point newPoint)
        {
            if (this.PointChanged != null)
            {
                this.PointChanged(this, oldPoint, newPoint);
            }
        }
        #endregion
    }

    /// <summary>
    /// 移动方向
    /// </summary>
    public enum MoveDirections
    {
        Both,
        X,
        Y
    }
}
